package info.monkeyws.AutoRinger;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

public class SleepTimeDialog extends DialogPreference {
	private SharedPreferences pref;
	private Editor editor;
	private TextView txt_sleep_begin_time;
	private TextView txt_sleep_end_time;
	private TimePicker start_picker;
	private TimePicker stop_picker;
	Context context;
	
	public SleepTimeDialog(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	protected void onPrepareDialogBuilder(Builder builder) {
		// TODO Auto-generated method stub
		pref = PreferenceManager.getDefaultSharedPreferences(context);
	    editor = pref.edit();
		
		LinearLayout layout = new LinearLayout(context);
		layout.setPadding(10, 0, 10, 0);
	    layout.setOrientation(1);
	    
	    start_picker = new TimePicker(context);
	    start_picker.setCurrentHour(pref.getInt("sleep_begin_hour", 0));
	    start_picker.setCurrentMinute(pref.getInt("sleep_begin_minute", 0));
	    txt_sleep_begin_time = new TextView(context);
	    txt_sleep_begin_time.setText(R.string.txt_sleep_begin_time);
	    txt_sleep_end_time = new TextView(context);
	    txt_sleep_end_time.setText(R.string.txt_sleep_end_time);
	    
	    stop_picker = new TimePicker(context);	    
	    stop_picker.setCurrentHour(pref.getInt("sleep_end_hour", 0));
	    stop_picker.setCurrentMinute(pref.getInt("sleep_end_minute", 0));
	    
	    layout.addView(txt_sleep_begin_time);
	    layout.addView(start_picker);
	    layout.addView(txt_sleep_end_time);
	    layout.addView(stop_picker);
	    builder.setView(layout);
	    		
		super.onPrepareDialogBuilder(builder);
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		// TODO Auto-generated method stub
		editor.putInt("sleep_begin_hour", start_picker.getCurrentHour());
		editor.putInt("sleep_begin_minute", start_picker.getCurrentMinute());
		editor.putInt("sleep_end_hour", stop_picker.getCurrentHour());
		editor.putInt("sleep_end_minute", stop_picker.getCurrentMinute());
		editor.commit();

		super.onDialogClosed(positiveResult);
	}	

}
